package com.eling.elcms.travel.service.impl;


import com.eling.elcms.basedoc.model.DriverInfo;
import com.eling.elcms.basedoc.model.PersonalInfo;
import com.eling.elcms.basedoc.service.impl.PersonalInfoManagerImpl;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.travel.dao.IDriverDao;
import com.eling.elcms.travel.model.Driver;
import com.eling.elcms.travel.service.IDriverManager;
import com.eling.elcms.travel.service.IRoomPersonalInfoManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DriverManagerImpl extends GenericManagerImpl<Driver, Long>  implements IDriverManager {

	IDriverDao driverDao;
	
	@Autowired
	private PersonalInfoManagerImpl personalImp;
	
	@Autowired
	private IRoomPersonalInfoManager roomPersonalInfoManager;
	
	@Autowired
	private IDeriverInfoManagerImpl driverInfoImpl;
	
	@Autowired
	public void setDriverFileDao(IDriverDao driverDao){
		this.dao = driverDao;
		this.driverDao = driverDao;
	}

	@Override
	public Driver saveDriver(Driver cond,PersonalInfo personalInfo,DriverInfo driverInfo) {
		
		//存储驾照信息
//		DriverInfo driverinfor = new DriverInfo();
//		driverinfor.setDriveBookDate(driverInfo.getDriveBookDate());
//		driverinfor.setDriveBookType(driverInfo.getDriveBookType());
//		driverinfor.setVersion(0);
		DriverInfo driver= driverInfoImpl.save(driverInfo);
		personalInfo.setDriverInfo(driver);
		//存储司机基础信息
		if(personalInfo.getIdNumber()!=null){
			if(personalInfo.getIdNumber() == ""){
				personalInfo.setIdNumber(null);
			}else{
				PersonalInfo pInfo = roomPersonalInfoManager.queryById(personalInfo.getIdNumber());
				if(pInfo == null){
					personalInfo.setVersion(0);
					personalInfo = personalImp.save(personalInfo);
				}else {
					if(personalInfo.getName()!=null){
						pInfo.setName(personalInfo.getName());
					}
					if(personalInfo.getSex()!=null){
						pInfo.setSex(personalInfo.getSex());
					}	
					if(personalInfo.getMobilePhone()!=null){
						pInfo.setMobilePhone(personalInfo.getMobilePhone());
					}	
					if(personalInfo.getBirthday()!=null){
						pInfo.setBirthday(personalInfo.getBirthday());
					}	
					if(personalInfo.getElectronicMail()!=null){
						pInfo.setElectronicMail(personalInfo.getElectronicMail());
					}	
					if(personalInfo.getHouseRegister()!=null){
						pInfo.setHouseRegister(personalInfo.getHouseRegister());
					}	
					if(personalInfo.getAddress()!=null){
						pInfo.setAddress(personalInfo.getAddress());
					}	
					if(personalInfo.getPhone()!=null){
						pInfo.setAddress(personalInfo.getPhone());
					}
					pInfo.setDriverInfo(driver);
					personalInfo = personalImp.save(pInfo);
				}
			}
		}else {
			personalInfo = personalImp.save(personalInfo);
		}
		
		cond.setPersonalInfo(personalInfo);
		return driverDao.save(cond);
	}
	
}
